//
//  ColorExt.swift
//  kano
//
//  Created by Sophia on 2024/8/14.
//

import UIKit

extension  UIColor {
    //MARK: instace method
    convenience init(_ hexValue: Int, alphaVaule: Float) {
        self.init(red: CGFloat((hexValue & 0xFF0000) >> 16 ) / 255,
                  green: CGFloat((hexValue & 0x00FF00) >> 8) / 255,
                  blue: CGFloat((hexValue & 0x0000FF) >> 16 ) / 255,
                  alpha: CGFloat(alphaVaule))
    }
    
    convenience init(_ hexValue: Int) {
        self.init(hexValue, alphaVaule: 1)
    }
    
    //MARK: class method
    //parameter： 0xFF0000
    class func ka_hexColor(_ hexValue: Int, alphaVaule: Float)  -> UIColor {
        return UIColor(red: CGFloat((hexValue & 0xFF0000) >> 16 ) / 255,
                       green: CGFloat((hexValue & 0x00FF00) >> 8) / 255,
                       blue: CGFloat(hexValue & 0x0000FF) / 255,
                       alpha: CGFloat(alphaVaule))
    }
    
    class func ka_hexColor(_ hexValue: Int)  -> UIColor {
        return ka_hexColor(hexValue, alphaVaule: 1)
    }
    
    //parameter： "#FF0000"
    class func ka_hexString(_ hexString: String)  -> UIColor {
        
        var cString = hexString.trimmingCharacters(in:CharacterSet.whitespacesAndNewlines).uppercased()
        
        if (cString.hasPrefix("#")) {
            let index = cString.index(cString.startIndex, offsetBy:1)
            cString = String(cString[index...])
        }
        
        if (cString.count != 6) {
            return UIColor.red
        }
        
        let rIndex = cString.index(cString.startIndex, offsetBy: 1)
        let rString = String(cString[cString.startIndex...rIndex])
        
        let gStartIndex = cString.index(cString.startIndex, offsetBy: 2)
        let gEndIndex = cString.index(cString.startIndex, offsetBy: 3)
        let gString = String(cString[gStartIndex...gEndIndex])
        
        let bIndex = cString.index(cString.endIndex, offsetBy: -2)
        let bString = String(cString[bIndex...])
        
        var r:CUnsignedLongLong = 0, g:CUnsignedLongLong = 0, b:CUnsignedLongLong = 0;
        Scanner(string: rString).scanHexInt64(&r)
        Scanner(string: gString).scanHexInt64(&g)
        Scanner(string: bString).scanHexInt64(&b)
        
        return UIColor(red: CGFloat(r) / 255.0, green: CGFloat(g) / 255.0, blue: CGFloat(b) / 255.0, alpha: CGFloat(1))
    }

    
    func ka_toImage() -> UIImage {
        let rect = CGRect(x: 0, y: 0, width: 1, height: 1)
        UIGraphicsBeginImageContext(rect.size)
        let context = UIGraphicsGetCurrentContext()
        context?.setFillColor(self.cgColor)
        context?.fill(rect)
        let image = UIGraphicsGetImageFromCurrentImageContext()
        UIGraphicsEndImageContext()
        return image!
    }
}

extension UIColor {
    //themecolor 66748A
    public class var color66748A: UIColor {
        return UIColor.ka_hexString("#66748A")
    }
    public class var themeColor: UIColor {
        return UIColor.ka_hexString("#5656E6")
    }
    
    public class var colorF0F2F5: UIColor {
        return UIColor.ka_hexString("#F0F2F5")
    }
    
    
    //MARK: white
    public class var colorFFF: UIColor {
        return UIColor.ka_hexString("#FFFFFF")
    }
    public class var color999: UIColor {
        return UIColor.ka_hexString("#999999")
    }
    
    public class var colorCBCACE: UIColor {
        return UIColor.ka_hexString("#CBCACE")
    }

    public class var colorf2f2f5: UIColor {
        return UIColor.ka_hexString("#f2f2f5")
    }
    
    public class var color77777D: UIColor {
        return UIColor.ka_hexString("#77777D")
    }
    public class var color323135: UIColor {
        return UIColor.ka_hexString("#323135")
    }
    
    public class var color8A8891: UIColor {
        return UIColor.ka_hexString("#8A8891")
    }
    public class var colorBDBDF5: UIColor {
        return UIColor.ka_hexString("#BDBDF5")
    }

    public class var color7E7EEC: UIColor {
        return UIColor.ka_hexString("#7E7EEC")
    }
    public class var colorE7E7FB: UIColor {
        return UIColor.ka_hexString("#E7E7FB")
    }
    public class var colorF4F4FD: UIColor {
        return UIColor.ka_hexString("#F4F4FD")
    }
    
    public class var color615F67: UIColor {
        return UIColor.ka_hexString("#615F67")
    }
    public class var colorFF4141: UIColor {
        return UIColor.ka_hexString("#FF4141")
    }
    
    
    public class var colorFF8921: UIColor {
        return UIColor.ka_hexString("#FF8921")
    }
    
    public class var colorFFE9D5: UIColor {
        return UIColor.ka_hexString("#FFE9D5")
    }
    
    
    public class var colorD5D5F9: UIColor {
        return UIColor.ka_hexString("#D5D5F9")
    }
    
    public class var colorFFD0BF: UIColor {
        return UIColor.ka_hexString("#FFD0BF")
    }
    
    
    public class var colorFF5B42: UIColor {
        return UIColor.ka_hexString("#FF5B42")
    }
    
    
    public class var colorAEACB3: UIColor {
        return UIColor.ka_hexString("#AEACB3")
    }
    
    public class var colorFF8A2A: UIColor {
        return UIColor.ka_hexString("#FF8A2A")
    }
    public class var colorE2E1E3: UIColor {
        return UIColor.ka_hexString("#E2E1E3")
    }
    public class var colorFFE8E8: UIColor {
        return UIColor.ka_hexString("#FFE8E8")
    }
    public class var colorFF3644: UIColor {
        return UIColor.ka_hexString("#FF3644")
    }
    
    public class var colorFFEAD6: UIColor {
        return UIColor.ka_hexString("#FFEAD6")
    }
    public class var colorFFF6E7: UIColor {
        return UIColor.ka_hexString("#FFF6E7")
    }
    public class var colorEE7B33: UIColor {
        return UIColor.ka_hexString("#EE7B33")
    }
    public class var colorF6F6F6: UIColor {
        return UIColor.ka_hexString("#F6F6F6")
    }
    
    public class var colorFFD7A9: UIColor {
        return UIColor.ka_hexString("#FFD7A9")
    }
    
    public class var colorBF441E: UIColor {
        return UIColor.ka_hexString("#BF441E")
    }
    
    public class var colorDB6D4C: UIColor {
        return UIColor.ka_hexString("#DB6D4C")
    }
    
    public class var colorFFFCFC: UIColor {
        return UIColor.ka_hexString("#FFFCFC")
    }
    
    public class var colorFFEEEE: UIColor {
        return UIColor.ka_hexString("#FFEEEE")
    }
    
    public class var colorEF6957: UIColor {
        return UIColor.ka_hexString("#EF6957")
    }
    
    public class var colorFFD771: UIColor {
        return UIColor.ka_hexString("#FFD771")
    }
    
    public class var colorFF5953: UIColor {
        return UIColor.ka_hexString("#FF5953")
    }
    
    public class var colorA19389: UIColor {
        return UIColor.ka_hexString("#A19389")
    }
        
    public class var colorFF485E: UIColor {
        return UIColor.ka_hexString("#FF485E")
    }

}
